package com.lzw.kotdemo.adapter

import android.support.v7.widget.AppCompatAutoCompleteTextView
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.lzw.kotdemo.R

import com.lzw.kotdemo.bean.ForecastList
import com.lzw.kotdemo.extensions.ctx
import com.lzw.kotdemo.model.Forecast
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find
import kotlinx.android.synthetic.main.item_forecast.view.*

/**kotDemo
 * Created by lzw on 2018/12/27. 15:46:28
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
class ForecastListAdapter(val weekForecast: ForecastList,val itemClick: (Forecast) -> Unit):
        RecyclerView.Adapter<ForecastListAdapter.ViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.ctx).inflate(R.layout.item_forecast,parent,false)

        return ViewHolder(view,itemClick)
    }

    override fun getItemCount(): Int =weekForecast.dailyForecast.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //with is a useful function, It basically receives an object and an extnension function as parameters
        //and makes the object execute the function
        holder.bindForeacast(weekForecast.dailyForecast[position])
    }

    class ViewHolder(view:View,val itemClick:(Forecast) -> Unit) : RecyclerView.ViewHolder(view){


        fun bindForeacast(forecast:Forecast){
            with(forecast){
                Picasso.get().load(iconUrl).into(itemView.icon)

                itemView.date.text = date
                itemView.description.text = description
                itemView.maxTemp.text = "${high.toString()}"
                itemView.minTemp .text = "${low.toString()}"
                itemView.setOnClickListener{itemClick(this)}
            }
        }

    }

}